package com.ch.android.lookforkotlinv2.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.ch.android.lookforkotlinv2.R
import com.ch.android.lookforkotlinv2.adapter.LiveListAdapter
import com.ch.android.lookforkotlinv2.databinding.FragmentLiveListBinding
import com.ch.android.lookforkotlinv2.vm.LiveListModel
import com.ch.android.networklib.data.LiveListItemData

class LiveListFragment() : Fragment() {
    private var liveTypeId: Int? = null
    private lateinit var mBinding: FragmentLiveListBinding
    private val items: MutableList<LiveListItemData> = mutableListOf()
    private lateinit var mAdapter: LiveListAdapter
    private var mViewModel: LiveListModel? = null
    private var pageNo = 1
    private val pageSize = 10

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            liveTypeId = it.getInt(LIVE_TYPE_ID)
        }

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.d("LiveTypeFragment", "onCreateView liveTypeId: $liveTypeId")
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_live_list, container, false)
        mBinding.lifecycleOwner = this // 这里的 this 指的是 Fragment

        mViewModel = LiveListModel(requireActivity().application)
        mBinding.model = mViewModel

        mAdapter = LiveListAdapter(items)
        mBinding.liveList.adapter = mAdapter
        mBinding.liveList.layoutManager = LinearLayoutManager(context)

        mBinding.refreshLayout.setOnRefreshListener {
            //下拉刷新后，重置pageNo
            pageNo = 1
            mBinding.model!!.loadData(liveTypeId!!, pageNo, pageSize)
        }
        mBinding.refreshLayout.setOnLoadMoreListener {
            mBinding.model!!.loadData(liveTypeId!!, ++pageNo, pageSize)
        }

        mBinding.model?.items?.observe(viewLifecycleOwner) {
            if (pageNo == 1 || mBinding.refreshLayout.isRefreshing) {
                //API请求完成后，设置下拉刷新完成
                mBinding.refreshLayout.finishRefresh()
                items.clear()
                items.addAll(it)
                mAdapter.notifyDataSetChanged()
            } else {
                //API请求完成后，设置上拉加载更多完成
                mBinding.refreshLayout.finishLoadMore()
                if (it.isNotEmpty()) {
                    items.addAll(it)
                    mAdapter.notifyItemRangeChanged(items.size - it.size, it.size)
                } else {
                    --pageNo
                }
            }
        }

        mBinding.model!!.loadData(liveTypeId!!, pageNo, pageSize)
        return mBinding.root
    }


    companion object {
        private const val LIVE_TYPE_ID = "liveTypeId"
        fun newInstance(liveType: Int): LiveListFragment {
            val fragment = LiveListFragment()
            val args = Bundle()
            args.putInt(LIVE_TYPE_ID, liveType)
            fragment.arguments = args
            return fragment
        }
/*
        @JvmStatic
        fun newInstance(userName: String, param2: Int) =
            DemoFragmentA().apply { arguments = Bundle().apply {
                    putString(USER_NAME, userName)
                    putInt(ARG_PARAM2, param2)
                }
            }
*/
    }
}